/* 
 * Author: anand
 * 
 */

#include "dbhelper.h"

#include "playlist.h"


playlist::playlist() {
    _dbHelper = dbhelper::get_helper();
	result_set * latest = _dbHelper->get_row_map(PLAYLIST_GET_LATEST_SQL);
	if(latest->size())
	{
		_id = atoi((*(latest->at(0)))["id"].c_str());
		_name = (*(latest->at(0)))["name"];
		_lastactivets = atoi((*(latest->at(0)))["lastactivets"].c_str());
	}
	else
	{
	   _name = "My Playlist";
	   _isNew = true;
	   save();
	}
    _playlist_items = new playlist_items(_id);
    _current = -1;
}

std::string playlist::next_item(){
    _current++;
    if(_current < _playlist_items->size())
        return std::string(_playlist_items->at(_current)->get_full_path());
    else{
        _current = -1; //reset
		return std::string();
    }
}
std::string playlist::prev_item(){

    if(_current <= 0)
        _current = _playlist_items->size() -1;
    else
        _current--;

    if(_current < _playlist_items->size())
        return std::string(_playlist_items->at(_current)->get_full_path());
    else
        return std::string(); //should never happen, but who knows for sure ;)
}
std::string playlist::current_item(){
    if(-1 < _current && _current < _playlist_items->size())
        return std::string(_playlist_items->at(_current)->get_full_path());
    else
		return std::string();
}

void playlist::raw_execute(const char *sql){
    _dbHelper->raw_execute(sql);
}

void playlist::show_available_playlists(){
	_dbHelper->raw_execute(PLAYLIST_GET_ALL_SQL);
}
void playlist::add_item(playlist_item* item){

}

void playlist::remove_item(playlist_item* item){

}

void playlist::universal_remove_item(playlist_item* item){
    
}

void playlist::delete_item(playlist_item* item){
    universal_remove_item(item);
    item->unlink();
}

void playlist::show_current_playlist()
{
	_playlist_items->show_playlist_items();
}

void playlist::add_item(char *path_, int position_)
{

	std::string path = path_util::get_folder_path(path_);
    std::string name = path_util::get_file_name(path_);

	double position = 1;
	if(_playlist_items->size() != 0 ) 
	{
		if(position_ == -1) //add whereever we like, just make sure this is current
		{
			position = _playlist_items->at(_playlist_items->size()-1)->get_position() + 0.5;
            //now there will be atlest two items, move to one above the latest
            _current = _playlist_items->size()-1;
		}
		else 
		{
			//TODO: use user input position
		}
	}

	playlist_item item;
	item.set_path(path); item.set_name(name); item.set_position(position);
	_playlist_items->add_playlist_item(&item);
}
void playlist::save()
{
	std::string sql;
    _lastactivets = time(NULL);
	if(_isNew){
		sql = PLAYLIST_INSERT_SQL;
		_id = _dbHelper->get_id(PLAYLIST_TABLE_NAME);
		sql.replace(sql.find("?"),1,path_util::convert_to_string(_id));
    	sql.replace(sql.find("?"),1,_name);
		sql.replace(sql.find("?"),1,path_util::convert_to_string(_lastactivets));
	}
	else{
		sql = PLAYLIST_UPDATE_SQL;
    	sql.replace(sql.find("?"),1,_name);
        sql.replace(sql.find("?"),1,path_util::convert_to_string(_lastactivets));
		sql.replace(sql.find("?"),1,path_util::convert_to_string(_id));
	}
	_dbHelper->raw_execute(sql.c_str());
	_isNew = false;
}
result_set * playlist::get_row_map(const char *sql)
{
	return _dbHelper->get_row_map(sql);
}

playlist::~playlist(){
    delete _playlist_items;
}
